nition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) 
    {
          // 我们首先使用层序遍历 之后逆序就可以
        vector<vector<int>> ans;
        vector<int> v;

        if (root == nullptr)
        {
            return v;
        }

        queue<TreeNode*> que;
        int levelsize = 1;
        que.push(root);
        while(!que.empty())
        {
            if (levelsize) // 该层还有数据 
            {
                TreeNode* top = que.front();
                que.pop();
                v.push_back(top->val);
                if (top->left)
                {
                    que.push(top->left);
                }

                if (top->right)
                {
                    que.push(top->right);
                }

                levelsize--;
            }
            else
            {
                ans.push_back(v);
                v.clear();
                levelsize = que.size();
            }
        }

        ans.push_back(v);

        vector<int> result;
        for (auto x : ans)
        {
            result.push_back(x.back());
        }

        return result;
    }
};
